<template>
	<view class="container">
		<u-form :model="feedbackForm" ref="uForm" :border-bottom="false">
			<u-form-item label="您的邮箱" label-width="150rpx" label-position="top" prop="from">
				<u-input v-model="feedbackForm.from" :border="border" />
			</u-form-item>
			<u-form-item label="您的意见" label-width="150rpx" label-position="top" prop="text">
				<u-input v-model="feedbackForm.text" :type="type" :border="border" :height="height" :auto-height="autoHeight" />
			</u-form-item>
		</u-form>
		<view class="but">
			<u-button type="primary" shape="circle" size="medium" :plain="true" @click="submit">提交</u-button>
		</view>
		<u-toast ref="feedbackToast" />
	</view>
</template>

<script>
	import api from '../../utils/api.js'
	export default {
		data() {
			return {
				toastTitle: '',
				toastType: '',
				feedbackForm: {
					from: '',
					text: ''
				},
				value: '',
				type: 'textarea',
				border: true,
				height: 300,
				autoHeight: true,
				rules: {
					from: [{
						required: true,
						message: '邮箱不能为空哦',
						trigger: 'change'
					}, ],
					text: [{
						required: true,
						message: '说一些内容吧(不能少于十个字)',
						min: 10,
						max: 500,
						trigger: 'change'
					}]
				},
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			showAddToast() {
				this.$refs.feedbackToast.show({
					title: this.toastTitle,
					type: this.toastType,
					position: 'top',
					duration: 1000
				})
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						uni.showLoading({
								mask: true,
						    title: '发送邮件中...'
						});
						api.postFeedback(this.feedbackForm).then((res) => {
							if (res.code === 0) {
								uni.hideLoading();
								this.toastTitle = "反馈成功";
								this.toastType = "success";
								this.showAddToast();
								setTimeout(function() {
									uni.reLaunch({
									  url: '../user/user'
									});
								}, 1300);
							}
						}).catch(() => {
							uni.hideLoading();
							this.toastTitle = "反馈失败, 请稍后重试";
							this.toastType = "error";
							this.showAddToast();
						})
					} else {
						this.toastTitle = "请按正确的规则填写评价";
						this.toastType = "warning";
						this.showAddToast();
					}
				})
			}
		}
	};
</script>



<style>
	.but {
		position: absolute;
		display: block;
		width: 50%;
		margin-top: 40%;
		margin-left: 30%;
	}

	.container {
		padding: 50rpx 50rpx 30rpx 30rpx;

	}
</style>
